<template>
  <el-container class="home-container">
    <el-header>
      <!-- <img src="../assets/heima.png" alt=""> -->
      <span>后台管理系统</span>
      <div class="flex-row-vcenter">
        <i
          class="cursor iconfont font25"
          :class="!isFullScreen ? 'icon-quanping_o' : 'icon-quxiaoquanping_o'"
          @click="toggleScreen"
        ></i>
        <i
          class="el-icon-s-tools font25 cursor ml-large"
          @click="toggleDrawer"
        ></i>
        <el-dropdown trigger="click" @command="handleCommand">
          <div class="el-dropdown-link flex-row-vcenter">
            <el-avatar icon="el-icon-user-solid" size="small"></el-avatar>
            <span class="user-name">{{ user.operName }}</span
            ><i class="el-icon-arrow-down el-icon--right"></i>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="">
              <i class="iconfont icon-danwei"></i> {{ user.orgName }}
            </el-dropdown-item>
            <el-dropdown-item command="logout" divided>
              <i class="iconfont icon-zhuxiao"></i> 注销
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <el-container class="custom-container">
      <el-aside :width="isCollapse ? '58px' : '220px'">
        <!-- <el-aside :width="isCollapse ? '220px' : '58px'"> -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边导航栏 -->
        <!-- 使用unique属性时，要绑定该属性，否则只能是unique-opened-->
        <el-menu
          background-color="#fff"
          text-color="#000"
          :unique-opened="false"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <template v-for="item in menulist">
            <!-- 一级导航-->
            <el-menu-item
              v-if="!item.children || !item.children.length"
              :index="item.path"
              :key="item.id"
              @click="savaNavPath(item.path)"
            >
              <i :class="item.iconFont"></i>
              <span>{{ item.authName }}</span></el-menu-item
            >
            <!-- 二级导航 -->
            <el-submenu v-else :index="item.id + ''" :key="item.id">
              <template slot="title">
                <i :class="item.iconFont"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级导航 -->
              <el-menu-item
                :index="subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="savaNavPath(subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-main>
        <keep-alive :exclude="noCacheRouter">
          <!-- 路由占位符 -->
          <transition name="scale" mode="out-in">
            <router-view v-slot="{ route, Component }">
              <component :key="route.name" :is="Component"></component>
            </router-view>
          </transition>
        </keep-alive>
      </el-main>
    </el-container>
    <el-drawer
      title="我是标题"
      :visible.sync="drawer"
      direction="rtl"
      :with-header="false"
      custom-class="p-large"
    >

    </el-drawer>
  </el-container>
</template>
<script>
import { menuData } from '@/api'
import { noCacheRouter } from '@/router/router-constant'
import operation from '@/components/bus/operation-mixin'
import { mapMutations } from 'vuex'
import screenfull from 'screenfull'
export default {
  mixins: [operation],
  data() {
    return {
      // 是否全屏
      isFullScreen: false,
      menulist: [], // 左侧菜单数据
      isCollapse: false,
      activePath: '',
      noCacheRouter: noCacheRouter,
      drawer: false
    }
  },
  created() {
    // screenfull是插件，它自带事件监听 on
    // screenfull.isFullscreen ：插件自带的属性，表示当前是否处于全屏状态
    screenfull.on('change', () => {
      console.log('当前是否是全屏', screenfull.isFullscreen)
      this.isFullScreen = screenfull.isFullscreen
    })
    this.getMenuList()
    // 组件创建的时候给activePath赋值
    this.activePath = window.sessionStorage.getItem('activePath') || '/welcome'
  },
  mounted() {
    window.addEventListener('keydown', this.KeyDown)
    this.$once('hook:beforeDestroy', () => {
      window.removeEventListener('keydown', this.KeyDown)
    })
  },
  methods: {
    ...mapMutations('user', {
      LOGOUT: 'LOGOUT'
    }),
    handleCommand(command) {
      switch (command) {
        case 'logout':
          this.logout()
          break
      }
    },
    logout() {
      this.LOGOUT()
      this.$router.push('/login')
    },
    async getMenuList() {
      // 获取侧边导航数据
      let res = null
      if (process.env.NODE_ENV === 'mock') {
        res = await menuData()
      } else {
        res = await this.$https('mock/menus.json')
      }
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menulist = res.data
    },
    toggleCollapse() {
      // 实现折叠与展开效果
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    savaNavPath(activePath) {
      // 新窗口打开大屏页面
      if (activePath.indexOf('data-visible') > -1) {
        let url = this.$router.resolve({
          path: activePath
        })
        window.open(url.href, '_blank')
        this.$router.go(-1)
        return false
      }
      console.log(activePath, '当前路由')
      // 使用sessionStorage保存侧边导航激活的状态
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath // 点击的时候给activePath赋值
    },
    // 全屏状态切换
    toggleScreen() {
      if (!screenfull.isEnabled) {
        return false
      }
      screenfull.toggle()
      this.isFullscreen = !this.isFullscreen
    },
    /**
     * @description 敲击键盘事件
     * @param {obj} event 按键信息
     * @return void
     */
    KeyDown(event) {
      console.log(event, '----按键信息')
      if (event.keyCode === 122) {
        event.returnValue = false
        this.toggleScreen()
      }
    },
    toggleDrawer() {
      this.drawer = !this.drawer
    }

  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-main {
  padding: 10px 10px;
}
.home-container {
  height: 100%;
}
.el-header {
  background-color: $--color-primary;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;

  span {
    margin-left: 15px;
  }
  .el-dropdown-link {
    color: #fff;
    cursor: pointer;
    .user-name {
      margin-left: 10px;
    }
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
}
.custom-container {
  height: calc(100% - 60px);
}

.el-aside {
  background-color: $--color-primary;
  height: 100%;
  .toggle-button {
    background-color: #fff;
    text-align: center;
    color: #000;
    font-size: 10px;
    height: 24px;
    line-height: 24px;
    letter-spacing: 0.2rem;
    cursor: pointer;
  }
  .el-menu {
    height: calc(100% - 24px);
    border-right: none;
    overflow: hidden;
    overflow-y: auto;
    border-top: 1px solid $--color-primary;
  }
  .iconfont {
    margin-right: 10px;
  }
}
.el-main {
  background-color: #eaedf1;
}

// slide styles!
.scale-enter-active,
.scale-leave-active {
  transition: all 0.5s linear;
}
.scale-leave-from,
.scale-enter-to {
  opacity: 1;
}
.scale-enter-from,
.scale-leave-to {
  opacity: 0;
}
</style>
